define(function(require, exports, module) {
	var _b = require('_b');
	var _ = require('_');
	var Cart = require('../models/cart');
	window.cart = new Cart();
	_b.Events.on('totalChange', function(collection){ 
		var total = 0;
		var unit = '$';
		for(var k in collection.models){
			unit = collection.models[k].get('unit');
			total+=parseFloat(collection.models[k].get('total'));
		}
		$('#sumTotal').html(unit+''+total);
	});
	var CartItemView = _b.View.extend({
		  tagName: "tr",
		  template:_.template($('#tableCartItem').html()),
		  events: {
			  'change .quanity':'quanityChange',
			  'click .deleteProduct':'deleteProduct',
			  'click .reductCartNum':'reductCartNum',
			  'click .addCartNum':'addCartNum'
		  },
		  deleteProduct:function(){
			  if(confirm('确认要从购物车中删除该商品么？')){
				  this.model.destroy();
				  $('#cart_num').html(parseInt($('#cart_num').html())-1);
			  }
		  },
		  addCartNum:function(){
			  var quanity = parseInt(this.model.get('num'))+1;
			  this.model.set('num' ,quanity);
			  this._quantiyChange(quanity);
		  },
		  reductCartNum:function(){
			  var quanity = parseInt(this.model.get('num'))-1;
			  this.model.set('num' ,quanity);
			  this._quantiyChange(quanity);
		  },
		  quanityChange:function(e){
			  var t = e.target;
			  var quanity = parseInt(t.value);
			  this._quantiyChange(quanity);
		  },
		  _quantiyChange:function(quanity){
			  var p = this.model.toJSON();
			  if(quanity!=NaN && quanity>0){
				  var total = quanity * parseFloat(p.price);
				  this.model.set('total',total);
				  this.model.set('num' ,quanity);
				  this.$el.find('.total_amount').html(total);
				  this.$el.find('.quanity').val(quanity);
//				  this.model.save(this.model.toJSON(),{
//					  url:HOST+'/pay/default/updateCart'
//				  });
				  this.model.save();
				  _b.Events.trigger('totalChange',this.collection);
			  }else{
				  this.$el.find('.quanity').val(p.num);
				  this.$el.find('.total_amount').html(p.total);
			  }
		  },
		  initialize: function() {
		  },
		  render: function() {
			  this.$el.html(this.template(this.model.toJSON()));
			  return this;
		  }
	});
	
	var CartListView = _b.View.extend({
		 el: $('#tableBody'),
		 initialize: function() {
			    this.listenTo(this.collection, "change", this.updateTotal);
			    this.listenTo(this.collection, "add", this.render);
			    this.listenTo(this.collection, "remove", this.render);
		 },
		  renderItem:function($model,k){
			  var itemview = new CartItemView({
		            model: $model,
		            collection:this.collection
		        }); 
			  this.collection.unit = $model.get('unit');
			  this.collection.total+=parseInt($model.get('total'));
			  this.$el.append( itemview.render().el );
		  },
		  render: function() {
			  this.collection.total = 0;
			  this.collection.unit = '$';
			  this.$el.html('');
			  if(this.collection.length == 0){
				  this.$el.append('<td colspan="6">暂无商品在购物车。。</td>');
				  $('#cartFooter').html('');
			  }else{
				  this.collection.each(this.renderItem,this);
				  $('#sumTotal').html(this.collection.unit+''+this.collection.total);
				  $('#cartFooter').html($('#tableFooter').html());
			  }
			  return this;
		  }
	});
	var listView = new CartListView({
		collection:cart
	});
	cart.fetch();
	listView.render();
});